<?php

defined('SYSPATH') or die('Access error!');

class Controller_Comment extends Controller_Q2a_Main
{

    private $comment;

    public function before()
    {
        parent::before();
        $this->comment = Model_Q2a_Comment::factory('q2a_comment');
        Breadcrumbs::add('问答平台', URL::site() . 'question/list/');
        I18n::set_current_file('question');
    }

    public function action_add()
    {
        $page =  Arr::get($_POST, 'page', '');
        if (!$this->logged_in())
        {
            Message::error('请先登录');
            $this->request->redirect(Route::get('user')->uri(array('action' => 'login')));
        }

        $data = array();
        $data['content'] = Arr::get($_POST, 'content', '');
        $data['parent_id'] = Arr::get($_POST, 'parent_id', 0);
        $data['user_id'] = Session::instance()->get_detail('user', 'id');
        $data['notify_email'] = Session::instance()->get_detail('user', 'email');
        $data['latest_activity'] = Date::get_time();
        $data['add_time'] = Date::get_time();
        $data['update_time'] = Date::get_time();
        $data['post_type'] = Q2A_POST_TYPE_COMMENT;
        $question_id = Arr::get($_POST, 'question_id');
        if ($this->comment->insert_comment($data))
        {
            if (intval($this->settings['q2a_comment_answer_points']) != 0)
            {
                Model_User_Points::factory('user_points')->save_points(Session::instance()->get_detail('user', 'id'), 'q2a_comment_answer_points',$question_id);
            }
            Message::success('回复成功');
            $this->request->redirect(Route::get('question')->uri(array('action' => 'detail', 'id' => $question_id . '?page='.$page.'#answer_' . $data['parent_id'])));
        }
    }

}

